﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using System.Text.RegularExpressions;

namespace Wicresoft.RDW.Mail
{
    public partial class Update : BasePage_BackEnd
	{
        #region 属性
        public int Count
        {
            get
            {
                if (ViewState["Count"] == null)
                {
                    ViewState["Count"] = 0;
                }
                return (int)ViewState["Count"];
            }
            set { ViewState["Count"] = value; }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            //FCKeditor1.BasePath = "/fckeditor/";
            //FCKeditor1.Value = "<p>This is some <strong>sample text</strong>. You are using <a href=\"http://www.fckeditor.net/\">FCKeditor</a>.</p>";
            btnSave.Text = Resources.NameResource.btnSave;
            btnReturn.Text = Resources.NameResource.btnCancel;
            if (!IsPostBack)
            {
                BindData();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Contents = this.FCKeditor1.Value; //Server.HtmlEncode(this.FCKeditor1.Value);
            //参数验证
            if (GetCount(Contents) == Count && TempEnabled(Contents))
            {
                //数据保存
                Model.MailTemplate obj = MailPresent.GetById(Id);
                obj.Name = this.lblName.Text;
                obj.Template = this.FCKeditor1.Value;// Server.HtmlEncode(this.FCKeditor1.Value);
                obj.Subject = this.tbxsubject.Text;
                MailPresent.Update(obj);
                Response.Redirect("View.aspx");
            }
            lblErrors.Text = "参数错误";
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("View.aspx");
        }
        #endregion

        #region 方法
        private void BindData()
        {
            Model.MailTemplate obj = MailPresent.GetById(Id);
            this.lblName.Text = obj.Name;
            this.FCKeditor1.Value = obj.Template; //Server.HtmlDecode(obj.Template);
            this.tbxsubject.Text = obj.Subject;
            Count = GetCount(obj.Template);

        }
        //得到参数数量
        private int GetCount(string coutents)
        {
            string str = coutents;// Server.HtmlEncode(coutents);
            string parrentStr = @"{(\d+)}";
            int count = 0;
            foreach (Match match in Regex.Matches(str, parrentStr))
            {
                string aba = match.Groups[1].Value;
                count = count + 1;
            }
            return count;
        }
        //第一个参数是否是0
        private bool TempEnabled(string coutents)
        {
            bool result = true;
            string str = coutents;// Server.HtmlEncode(coutents);
            string parrentStr = @"{(\d+)}";
            foreach (Match match in Regex.Matches(str, parrentStr))
            {
                string aba = match.Groups[1].Value;
                if (aba != "0")  //第一个参数必须是0
                {
                    result = false;
                }
                break;
            }
            return result;
        }

        private string GetBasePath()
        {
            string path = Request.Url.AbsolutePath;

            return path;
        }
        #endregion
	}
}
